package com.gr.staffpm.charts;

import java.io.IOException;

import org.apache.wicket.Resource;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.image.NonCachingImage;
import org.apache.wicket.markup.html.image.resource.DynamicImageResource;
import org.apache.wicket.model.IModel;
import org.apache.wicket.util.io.ByteArrayOutputStream;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class JFreeChartImageWithToolTip extends NonCachingImage {

    private static final long serialVersionUID = 1L;
    private static final Logger logger = LoggerFactory.getLogger(JFreeChartImageWithToolTip.class);
    private final String imageMapId;
    private final int width;
    private final int height;
    private final ChartRenderingInfo chartRenderingInfo = new ChartRenderingInfo(new StandardEntityCollection());

    public JFreeChartImageWithToolTip(String id, IModel<JFreeChart> model, String imageMapId, int width, int height) {
        super(id, model);
        this.imageMapId = imageMapId;
        this.width = width;
        this.height = height;
    }

    @Override
    protected Resource getImageResource() {
        Resource imageResource = null;
        final JFreeChart chart = (JFreeChart) getDefaultModelObject();
        imageResource = new DynamicImageResource() {

            private static final long serialVersionUID = 1L;

            @Override
            protected byte[] getImageData() {
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                try {
                    if (chart != null) {
                        chartRenderingInfo.clear();
                        ChartUtilities.writeChartAsPNG(stream, chart, width, height, chartRenderingInfo);
                    }
                } catch (IOException ex) {
                    logger.error("Error occured while creating chart", ex);
                }
                return stream.toByteArray();
            }
        };
        return imageResource;
    }

    @Override
    protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
        JFreeChart chart = (JFreeChart) getDefaultModelObject();
        if (chart == null) {
            return;
        }
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            if (chart != null) {
                chartRenderingInfo.clear();
                ChartUtilities.writeChartAsPNG(stream, chart, width, height, chartRenderingInfo);
            }
        } catch (IOException ex) {
            logger.error("Error occured while creating chart", ex);
        }
        replaceComponentTagBody(markupStream, openTag, ChartUtilities.getImageMap(imageMapId, chartRenderingInfo));
    }
}
